package com.itlong.cloud.controller.parking;

import com.itlong.cloud.POJO.DTO.property.*;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyMonthCardRuleGetOneVO;
import com.itlong.cloud.POJO.VO.property.PropertyMonthCardRuleGetPageVO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleDeleteDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleGetOneDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleSaveDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyMonthCardRuleUpdateDTO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.project.property.IPropertyMonthCardRuleServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      智慧停车-月卡缴费规则管理
 * </desc>
 *
 * @createDate 2018/11/07
 */
@RestController
@RequestMapping("/parking/monthCard")
public class PropertyMonthCardRuleController extends BaseController {

    @Autowired
    private IPropertyMonthCardRuleServiceClient iPropertyMonthCardRuleServiceClient;

    /**
     * <desc>
     *      添加月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleSaveDTO 添加月卡缴费规则DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @ResponseBody
    @PostMapping("save")
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_MONTH_CARD_RULE_SAVE_LOG_DESC)
    @AuthAccessRequired
    public Object saveMonthCardRule(PropertyMonthCardRuleSaveDTO propertyMonthCardRuleSaveDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMonthCardRuleSaveDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMonthCardRuleSaveDTO.class, request);
            if (propertyMonthCardRuleSaveDTO == null || StringUtils.isBlank(propertyMonthCardRuleSaveDTO.getRuleName()) || StringUtils.isBlank(propertyMonthCardRuleSaveDTO.getMoney())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyMonthCardRuleSaveDTO.getProjectId())) {
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            PropertyMonthCardRuleGetOneDTO propertyMonthCardRuleGetOneDTO = new PropertyMonthCardRuleGetOneDTO();
            BeanUtils.copyProperties(propertyMonthCardRuleSaveDTO,propertyMonthCardRuleGetOneDTO);
            PropertyMonthCardRuleGetOneVO oneMonthCardRule = iPropertyMonthCardRuleServiceClient.getOneMonthCardRule(propertyMonthCardRuleGetOneDTO);
            if (oneMonthCardRule != null) {
                return new MessageVO(PropertyErrorCodeEnum.PROPERTY_MONTH_CARD_RULE_NAME_EXIST_ERR.getErrorCode());
            }
            if (!propertyMonthCardRuleSaveDTO.getMoney().matches("^([1-9][0-9]{0,5}|0)(\\.\\d{1,2})?$")) {
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iPropertyMonthCardRuleServiceClient.saveMonthCardRule(propertyMonthCardRuleSaveDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业-智慧停车】添加月卡缴费规则失败",e);
        }
    }

    /**
     * <desc>
     *      修改月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleUpdateDTO 修改月卡缴费规则DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @ResponseBody
    @PostMapping("update")
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_MONTH_CARD_RULE_UPDATE_LOG_DESC)
    @AuthAccessRequired
    public Object updateMonthCardRule(PropertyMonthCardRuleUpdateDTO propertyMonthCardRuleUpdateDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMonthCardRuleUpdateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMonthCardRuleUpdateDTO.class, request);
            if (propertyMonthCardRuleUpdateDTO == null || StringUtils.isBlank(propertyMonthCardRuleUpdateDTO.getRuleName()) || StringUtils.isBlank(propertyMonthCardRuleUpdateDTO.getMoney())
                    || StringUtils.isBlank(propertyMonthCardRuleUpdateDTO.getMonthCardId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyMonthCardRuleUpdateDTO.getProjectId())) {
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            PropertyMonthCardRuleGetOneDTO propertyMonthCardRuleGetOneDTO = new PropertyMonthCardRuleGetOneDTO();
            BeanUtils.copyProperties(propertyMonthCardRuleUpdateDTO,propertyMonthCardRuleGetOneDTO);
            PropertyMonthCardRuleGetOneVO oneMonthCardRule = iPropertyMonthCardRuleServiceClient.getOneMonthCardRule(propertyMonthCardRuleGetOneDTO);
            if (oneMonthCardRule != null) {
                return new MessageVO(PropertyErrorCodeEnum.PROPERTY_MONTH_CARD_RULE_NAME_EXIST_ERR.getErrorCode());
            }
            if (!propertyMonthCardRuleUpdateDTO.getMoney().matches("^([1-9][0-9]{0,5}|0)(\\.\\d{1,2})?$")) {
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iPropertyMonthCardRuleServiceClient.updateMonthCardRule(propertyMonthCardRuleUpdateDTO);
            return new MessageVO(propertyMonthCardRuleUpdateDTO.getMonthCardId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业-智慧停车】修改月卡缴费规则失败",e);
        }
    }

    /**
     * <desc>
     *      删除月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleDeleteDTO 删除月卡缴费规则DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @ResponseBody
    @PostMapping("delete")
    @LoggerInfo(operateDesc = LogDescConstant.PARKING_MONTH_CARD_RULE_DELETE_LOG_DESC)
    @AuthAccessRequired
    public Object deleteMonthCardRule(PropertyMonthCardRuleDeleteDTO propertyMonthCardRuleDeleteDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMonthCardRuleDeleteDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMonthCardRuleDeleteDTO.class, request);
            if (propertyMonthCardRuleDeleteDTO == null || StringUtils.isBlank(propertyMonthCardRuleDeleteDTO.getMonthCardId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyMonthCardRuleDeleteDTO.getProjectId())) {
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            Integer carCount = iPropertyMonthCardRuleServiceClient.getCarCountByMonthCardId(propertyMonthCardRuleDeleteDTO.getMonthCardId());
            if (carCount > 0) {
                return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),carCount);
            }
            iPropertyMonthCardRuleServiceClient.deleteMonthCardRule(propertyMonthCardRuleDeleteDTO);
            return new MessageVO(propertyMonthCardRuleDeleteDTO.getMonthCardId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业-智慧停车】删除月卡缴费规则失败",e);
        }
    }

    /**
     * <desc>
     *      查询单条月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleGetOneDTO 查询单条月卡缴费规则DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @ResponseBody
    @PostMapping("getOne")
    public Object getOneMonthCardRule(PropertyMonthCardRuleGetOneDTO propertyMonthCardRuleGetOneDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMonthCardRuleGetOneDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMonthCardRuleGetOneDTO.class, request);
            if (propertyMonthCardRuleGetOneDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyMonthCardRuleGetOneDTO.getMonthCardId()) && StringUtils.isBlank(propertyMonthCardRuleGetOneDTO.getRuleName())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            PropertyMonthCardRuleGetOneVO propertyMonthCardRuleGetOneVO = iPropertyMonthCardRuleServiceClient.getOneMonthCardRule(propertyMonthCardRuleGetOneDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),propertyMonthCardRuleGetOneVO);
        } catch (Exception e) {
            throw new DataAccessException("【物业-智慧停车】查询单条月卡缴费规则失败",e);
        }
    }

    /**
     * <desc>
     *      分页查询月卡缴费规则
     * </desc>
     *
     * @param propertyMonthCardRuleGetPageDTO 查询单条月卡缴费规则DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/08
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping("getPage")
    public Object getMonthCardRulePage(PropertyMonthCardRuleGetPageDTO propertyMonthCardRuleGetPageDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMonthCardRuleGetPageDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMonthCardRuleGetPageDTO.class, request);
            if (propertyMonthCardRuleGetPageDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyMonthCardRuleGetPageDTO.getProjectId())) {
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            Page<PropertyMonthCardRuleGetPageVO> page = iPropertyMonthCardRuleServiceClient.getMonthCardRulePage(propertyMonthCardRuleGetPageDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        } catch (Exception e) {
            throw new DataAccessException("【物业-智慧停车】查询月卡缴费规则列表失败",e);
        }
    }

}
